fatalfrontierfandomcom-20200215-history
User blog:OKRedleg/JSON Parser
For the sake of establishing the health and functionality of this wiki, the tools used to maintain the site need to be available and survive the personnel working them. For that purpose, if the event that I find my way out of the game and this wiki, I'm posting my Ruby Script used to parse the JSON files to extract the data. In order to run this script, you will need to download |Ruby 1.9.x. Installation *Copy and paste either of the two versions of the code below into a text editor and save it with a .rb file extension. *place the 'bb_game_data.json' file in the same directory as the script *run the script from the command prompt using 'ruby .rb' **If you used the Windows Installer for Ruby, then you can double click on the script to run it. *... *profit You should now have a seperate CSV file for each section in the JSON file for importing into a spreadsheet program. Documented Script #!/usr/bin/env ruby #This file parses the JSON file and creates 15 comma seperated files #to be used in spreadsheets like LibreOffice Calc, Excel, or Google Docs. #after running this program, the 15 local file will be overwritten with the new #data, so if you want to keep your existing local files, move them. #Once you have the 15 files you want, import them into your spreadsheet program. require 'json' #Require the json gem to parse JSON Files require 'csv' #Module to manipulate data into csv format #Make sure you have a the json file in the same directory as this ruby script #either name the file "ff_game_data.json" #or change the test assigned to file below file = "ff_game_data.json" #open the file and parse the JSON into a Hash of Arrays of Hashes!?!? ff = JSON.parse(File.open(file).read) #ff is now a Hash of Arrays of Hashes of values begin #Run this loop for each (15 total) key and hash (which contains an array) ff.each{|k,h| #Create and blank an array to hold the comma seperated data csv = [] #add the keys to the csv files for column headers csv << h0.keys #Run this loop for each array in the above hash h.each{|arr| #before we write an array to the file, lets group each row together in holder holder = [] #Assign each value in the array (without keys) to holder arr.each_value{|v| holder << v } #dump the holder array to csv csv << holder } #create a file locally named after the original hash key #i.e. Region, Cards, etc. #the "w" means write. All identically named files will be overwritten #change this to "a" if you want to append the data. I recommend leaving #it alone or you will end up with dupliate records. filename = k+".csv" out = CSV.open(filename, "w") #write the csv arrays to the file csv.each{|item| out << item} #and close the file out.close() } rescue "Something has gone wrong, check your files!" else puts "Files created successfully!" end I tend to over document my programs so that others can readily determine what each line of code does. Here is the same script without documentation. Undocumented Script require 'json' require 'csv' file = "ff_game_data.json" ff = JSON.parse(File.open(file).read) begin ff.each{|k,h| csv = [] csv << h0.keys h.each{|arr| holder = [] arr.each_value{|v| holder << v } csv << holder } filename = k+".csv" out = CSV.open(filename, "w") csv.each{|item| out << item} out.close() } rescue "Something has gone wrong, check your files!" else puts "Files created successfully!" end Category:Blog posts